#!/bin/bash
# Script for easy and SAFE Kdenlive & ffmpeg & mlt & mlt++ build utilizing LD_RUN_PATH
# All subprojects are compiled and installed to specified non-system directory.
# You can safely keep multiple kdenlive/ffmpeg versions.
#
# This sript ensures, that kdenlive will call only desired ffmpeg and mlt libs.
# Using LD_RUN_PATH is a better, more general, alternative to statically link it all together.
# You can verify this it by calling
# cd ~/build/kdenlive/bin && ldd ./kdenlive
# - or -
# cd ~/build/kdenlive/bin && LD_DEBUG=libs ./kdenlive
# Set DEST_DIR in script, otherwise ~/build/kdenlive is used by default.
#
# Espinosa, Aleksander Adamowski (Olo)
# $Id$
#
#
# Usage:
#      kdenlive_builder getsources - call svn to get sources for all subprojects for the first time
#      kdenlive_builder updatesources - call svn to update sources for all subprojects
#      kdenlive_builder clean      - call make clean for all subprojects
#      kdenlive_builder build      - call configure and make and make install for all subprojects
#      kdenlive_builder info       - print revision numbers and dates for each component


# Modify the destination directory if you want
# Or you can copy it afterwars to /opt/kdenlive-<version> or wherever you want.
# Caution: if you pick /usr here, you system kdenlive, ffmpeg  and mlt will be overwritten!
export DEST_DIR=~/build/kdenlive.$(date +%F_%H_%M)

case "$1" in

"getsources")
# Download sources of ffmpeg, mtl, mlt++, kdenlive from their SVNs
# Call this as a very first command, call it once, then use updatesources (quicker)
# Subdirectories are created by this command
svn co svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg &&
svn co https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt mlt &&
svn co https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt++ mlt++ &&
svn co https://kdenlive.svn.sourceforge.net/svnroot/kdenlive/trunk/kdenlive
;;

"updatesources")
# The same as 'getsources' but quicker
# You must have already all sorces downloaded prior to call this!
cd ffmpeg &&
svn update &&
cd ../mlt &&
svn update &&
cd ../mlt++ &&
svn update &&
cd ../kdenlive &&
svn update
;;

"clean")
cd ffmpeg
make clean
cd ../mlt
make clean
cd ../mlt++
make clean
cd ../kdenlive
make clean
;;

"build")
export PATH=$DEST_DIR/bin:$PATH
export LD_RUN_PATH=../lib

cd ffmpeg &&

#
# FFmpeg configuration settings explained:
#
# Note: Most of the options mean that an external library is required! Remember, many codecs, especially audio codecs,
# are NOT implemented by FFmpeg, or the implementation is inferior.
#
# --enable-gpl        .. enable GPLed libraries, like swscaller - ESSENTIAL, REQUIRED by MLT
# --enable-swscaler   .. enable SW scaller, statically linked lib handled by ffmpeg but GPL - ESSENTIAL, REQUIRED by MLT
# --enable-libmp3lame .. support for mp3 audio codec - STRONGLY RECOMMENDED
# --enable-liba52     .. support for ACC (aka A52) audio codec used in DVDs - STRONGLY RECOMMENDED
# --enable-libogg     .. support for OGG format - RECOMMENDED
# --enable-pp         .. enable post processing of video - required??
# --enable-libtheora  .. support for theora video codec by external lib - RECOMMENDED
# --enable-libvorbis  .. support for theora video codec by external lib - RECOMMENDED
# --enable-libx264    .. support for high quality H264 codec, extern. lib - GOOD TO HAVE
# --enable-libfaad    .. support for Advaced Audio codec - AAC (iPod, some mp4) - GOOD TO HAVE
# --enable-libfaac    .. altern. support for Advaced Audio codec - AAC (iPod, some mp4) - GOOD TO HAVE
#
# --enable-vhook      .. have no idea what is this good for, set on by default
# --enable-x11grab    .. have no idea what is this good for, set on by default
#
# --enable-libgsm     .. support for low bandwith GSM audio codec, mostly speach, not much used in movies
# --enable-xvid       .. Xvid codec support is well handled by FFmpeg itself - NOT RECOMMENDED
# --enable-amr_nb     .. support for audio codec, not widely used ?
# --enable-amr_wb     .. support for audio codec, not widely used ?
# --enable-libdts     .. support for audio? codec, not widely used ?
#
# --enable-debug      .. DEBUGGING support is swith ON by default. For a final distribution wise version remove this paramater
#

./configure --prefix=$DEST_DIR \
--enable-gpl \
--enable-shared \
--enable-swscaler \
--enable-libogg \
--enable-pp \
--enable-libtheora \
--enable-libmp3lame \
--enable-libvorbis \
--enable-liba52 \
--enable-libx264 \
--enable-debug &&
make &&
make install &&

cd ../mlt &&
./configure --prefix=$DEST_DIR --enable-gpl --avformat-swscale --enable-motion-est --enable-mmx --enable-debug &&
make &&
make install &&


cd ../mlt++ &&
./configure --prefix=$DEST_DIR --enable-debug &&
make &&
make install &&


cd ../kdenlive &&
sh bootstrap &&
./configure --prefix=$DEST_DIR --enable-debug=full &&
make &&
make install &&
[ -d ~/.kde/share/mimelnk/application ] &&
cp $DEST_DIR/share/mimelnk/application/vnd.kde.kdenlive.desktop ~/.kde/share/mimelnk/application/ &&
cp $DEST_DIR/share/mimelnk/application/vnd.kde.kdenlive.scenelist.desktop ~/.kde/share/mimelnk/application/
;;

"info")
# Print SVN revision number and udate date for each component
# Good to know when reporting an error
cd ffmpeg
echo "FFmpeg SVN version:"
svn info | grep '\(Revision\|Last\ Changed\ Date\)'
cd ../mlt
echo "MLT SVN version:"
svn info | grep '\(Revision\|Last\ Changed\ Date\)'
cd ../mlt++
echo "MLT++ SVN version:"
svn info | grep '\(Revision\|Last\ Changed\ Date\)'
cd ../kdenlive
echo "Kdenlive SVN version:"
svn info | grep '\(Revision\|Last\ Changed\ Date\)'
;;

*)
# print some help
echo 'Kdenlive & ffmpeg & mlt & mlt++ build script utilizing LD_RUN_PATH'
echo 'Set DEST_DIR in script, otherwise ~/build/kdenlive is used by default'
echo 'Usage: '
echo '      kdenlive_builder getsources - call svn to get sources for all subprojects for the first time'
echo '      kdenlive_builder updatesources - call svn to update sources for all subprojects'
echo '      kdenlive_builder clean - clean subdirs. Recommended to call prior to build'
echo '      kdenlive_builder build - build and instal all sources to user defined directory'
echo '      kdenlive_builder info  - print revision numbers and dates for each component'
;;

esac

# Changelog:
# v1.0 - 16.6.2007
#    initial version
# v2.0 - 27.6.2007
#    support for debugging
#    fixes by Olo
#    better ffmpeg help
#    revision number printing, etc.
# SVN revision 3 - 2007-07-07
# import into SVN on Google project hosting

